package com.jader.service;

import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jader.activities.MyTendsActivity;
import com.jader.beans.Response;
import com.jader.beans.User;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class TendsService extends Service {

	private NotificationManager manager;
	private boolean flag = true;
	private MyThread myThread;

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	public void onCreate() {
		this.myThread = new MyThread();
		this.myThread.start();
		super.onCreate();

	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		flag = true;
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		this.flag = false;
		super.onDestroy();

	}

	private void notification(List<Response> liebiaos) {
		manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0,
				new Intent(this, MyTendsActivity.class), 0);
		// 通过Notification.Builder来创建通知，注意API Level
		// API16之后才支持
		String text = "您有" + liebiaos.size() + "条未读消息，点击查看详情";
		Notification notify3 = new Notification.Builder(this)
				.setSmallIcon(R.drawable.xiao)
				.setTicker("玉缘:" + "您有新的回复，点击查看详情！").setContentText(text)
				.setContentIntent(pendingIntent3).setNumber(1).build(); // 需要注意build()是在API
		// level16及之后增加的，API11可以使用getNotificatin()来替代
		notify3.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时，通知将被清除。
		notify3.defaults = Notification.DEFAULT_SOUND;
		notify3.defaults |= Notification.DEFAULT_VIBRATE;
		notify3.defaults |= Notification.DEFAULT_LIGHTS;
		manager.notify(1, notify3);// 步骤4：通过通知管理器来发起通知。如果id不同，则每click，在status哪里增加一个提示

	}

	private class MyThread extends Thread {
		@Override
		public void run() {
			while (flag) {
				try {
					// 每个1秒向服务器发送一次请求
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				// 采用get方式向服务器发送请求
				sendRequest();
			}
		}
	}

	private void sendRequest() {
		String url = JaderApplication.getIP() + "/Jader/client";
		RequestParams params = new RequestParams();
		params.addBodyParameter("type", JaderApplication.SELECT_WEI_DU_XIAOXI
				+ "");
		User user = JaderApplication.user;
		if (user != null) {
			params.addBodyParameter("userid", user.getUserid() + "");
			JaderApplication.httpUtils.send(HttpMethod.POST, url, params,
					new RequestCallBack<String>() {

						@Override
						public void onFailure(HttpException arg0, String arg1) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onSuccess(ResponseInfo<String> arg0) {
							Gson gson = new Gson();
							Type typelist = new TypeToken<List<Response>>() {
							}.getType();
							List<Response> liebiaos = gson.fromJson(
									arg0.result, typelist);
							if (liebiaos.size() > 0) {
								notification(liebiaos);
								flag = false;
							}

						}
					});
		}
	}

}
